#!/usr/bin/env python
"""
:copyright:
Copyright (C) 2007 Association of Universities for Research in Astronomy (AURA)

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    3. The name of AURA and its representatives may not be used to
      endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AURA ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL AURA BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

:Author: Christopher Hanley 

:Purpose: 
GUI application used to create KMZ files used by GoogleSky
from input FITS or image files.

:Input:
1) A single FITS image with valid WCS
2) A single FITS image with valid WCS and registered color image
3) Three FITs images representing 3 color bands.

Any of the 3 possible inputs will require an ouput file name.

:Outout:
A kmz file containing all the data necessary for the images to
appear registered in GoogleSky


"""

__docformat__ = 'restructuredtext'

# Dependencies

import Tkinter as tk
import add2skycore as a2s

class Menu(tk.Frame):
    def __init__(self):
        # Define the main window frame
        tk.Frame.__init__(self,bg='black')
        self.pack(expand=tk.YES, fill=tk.BOTH)
        
        # Define label for the application window
        self.master.title('add2sky: Space Telescope Science Insitute')
        self.master.iconname("add2sky")
        
        # Define gsky graphic for the top of the page
        photo = tk.PhotoImage(file=a2s.__file__[:-12]+"add2sky.gif")
        headerlabel = tk.Label(self,image=photo,bg="black")
        headerlabel.image=photo
        headerlabel.grid(row=0, column=0, columnspan=2, sticky=tk.W+tk.E+tk.N+tk.S, padx=5, pady=5)
        
        # Define introductory text for the application
        intro = tk.Label(self,text="\n\nsomething good about GoogleSky and add2sky goes here...",fg="white",bg="black")
        intro.grid(row=0, column=2, columnspan=2, sticky=tk.W+tk.E+tk.N+tk.S, padx=5, pady=5)

        
        # Define radio button variable
        self.radioVar = tk.IntVar()
        
        # Set default option
        self.radioVar.set(1)

        # Define option 1
        tk.Radiobutton(self,text="Single FITS",value = 1, variable = self.radioVar, bg="black",fg="white",indicatoron=0).grid(row=2,column=0,sticky=tk.E)
        self.opt1val = tk.StringVar()
        tk.Entry(self,width=40,textvariable=self.opt1val,bg="grey",fg="black").grid(row=2,column=1,padx=1,pady=5,sticky=tk.W)
        self.opt1val.set("FITS")
        
        # Define option 2
        tk.Radiobutton(self,text="FITS and Image",value = 2, variable = self.radioVar, bg="black",fg="white",indicatoron=0).grid(row=3,column=0,sticky=tk.E)
        self.opt2vala = tk.StringVar()
        self.opt2valb = tk.StringVar()
        tk.Entry(self,width=40,textvariable=self.opt2vala,bg="grey",fg="black").grid(row=3,column=1,padx=1,pady=1,sticky=tk.W)
        self.opt2vala.set("FITS")
        tk.Entry(self,width=40,textvariable=self.opt2valb,bg="grey",fg="black").grid(row=3,column=2,padx=1,pady=1,sticky=tk.W)
        self.opt2valb.set("GRAPHIC(JPG,PNG)")
        
        ## Define option 3
        #tk.Radiobutton(self,text="3 FITS for Color Image",value = 3, variable = self.radioVar, bg="black",fg="white",indicatoron=0).grid(row=4,column=0,sticky=tk.E)
        #self.opt3vala = tk.StringVar()
        #self.opt3valb = tk.StringVar()
        #self.opt3valc = tk.StringVar()
        #tk.Entry(self,width=40,textvariable=self.opt3vala,bg="grey",fg="black").grid(row=4,column=1,padx=1,pady=5,sticky=tk.W)
        #self.opt3vala.set("FITS")
        #tk.Entry(self,width=40,textvariable=self.opt3valb,bg="grey",fg="black").grid(row=4,column=2,padx=1,pady=5,sticky=tk.W)
        #self.opt3valb.set("FITS")
        #tk.Entry(self,width=40,textvariable=self.opt3valc,bg="grey",fg="black").grid(row=4,column=3,padx=1,pady=5,sticky=tk.W)
        #self.opt3valc.set("FITS")
        
        # Define entry field for output file name
        tk.Label(self,text="Output Filename:",bg="black",fg="white").grid(row=4,column=0,pady=20,sticky=tk.E)
        self.outputname=tk.StringVar()
        tk.Entry(self,width=35,textvariable=self.outputname,bg="grey",fg="black").grid(row=4,column=1,pady=20,sticky=tk.W)
        self.outputname.set("new_input.kmz")
        
        # Define Buttons
        tk.Button(self,text="CREATE",state=tk.ACTIVE,command=lambda : self._add2skycore(self.radioVar.get()),width=20,relief=tk.RIDGE).grid(row=4,column=2,padx=10,pady=20,sticky=tk.W)
        tk.Button(self,text="HELP",state=tk.DISABLED,width=13,relief=tk.RIDGE).grid(row=6,column=2,pady=20,sticky=tk.E)
        tk.Button(self,text="EXIT",state=tk.ACTIVE,command=lambda : self.quit(),width=10,relief=tk.RIDGE).grid(row=6,column=3,pady=20,sticky=tk.W)

    def _add2skycore(self,radioVar):
        # Read the name of the target output file.
        output = self.outputname.get()

        if radioVar == 1:
            print "Option 1 selected!"
            input = self.opt1val.get()
            self._option1(input,output)
            self.quit()
        elif radioVar == 2:
            print "Option 2 selected!"
            input1 = self.opt2vala.get()
            input2 = self.opt2valb.get()
            self._option2(input1,input2,output)            
            self.quit()
        elif radioVar == 3:
            print "option 3 selected!"
            self.quit()
        else:
            print "Not an option!!!"
            self.quit()
    
    def _option1(self,input,output):
        # Single FITS input case
        obj = a2s.add2sky()
        print "Working on image: ",input
        obj.readSingleFits(input)
        print "File, ",input," has been read."
        obj.setImage()
        print "Creating PNG file..."
        obj.setOutput(output)
        print "Creaing output file...",output
        obj.createKMZ()
        print "Done!"

    def _option2(self,input1,input2,output):
        # Single FITS input case
        obj = a2s.add2sky()
        print "Working on image: ",input1
        obj.readSingleFits(input1)
        print "File, ",input1," has been read."
        obj.readColorImage(input2)
        print "File, ",input2," has been read."
        obj.setOutput(output)
        print "Creaing output file...",output
        obj.createKMZ()
        print "Done!"

if __name__ == '__main__':
    Menu().mainloop()

